<template>
  <div>
    <el-tree
      ref="refTree"
      :data="permissionData"
      :props="{label:'name'}"
      default-expand-all
      :show-checkbox="true"
      :check-strictly="true"
      node-key="id"
    />
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permisson'
import { tranListToTreeData } from '@/utils'
import { assignPermissions, getRoleDetail } from '@/api/setting'
export default {
  name: 'VueHrAssignPermission',
  props: {
    roleId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      permissionData: [] // 存储权限数据
    }
  },
  created() {
    this.loadPermissionList()
    this.loadPermissionByRoleId()
  },

  methods: {
    async loadPermissionList() {
      const res = await getPermissionList()
      console.log(res, '权限列表')
      this.permissionData = tranListToTreeData(res.data)
      console.log(this.roleId)
    },
    async loadPermissionByRoleId() {
      const res = await getRoleDetail(this.roleId)
      console.log(res, '当前角色权限数据')
      this.$refs.refTree.setCheckedKeys(res.data.permIds)
    },
    async hSave() {
      const permIds = this.$refs.refTree.getCheckedKeys()
      // console.log('当前选中的节点数组是', permIds)
      const res = await assignPermissions({
        id: this.roleId,
        permIds
      })
      console.log('保存角色的权限点的结果是', res)

      // 提示
      this.$message.success('保存角色的权限成功')
      // 关闭弹层
      this.hCancel()
    },
    hCancel() {
      // 通过父组件去关闭弹层
      this.$emit('close-dialog')

      // 清空当前的选择
      this.$refs.refTree.setCheckedKeys([])
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
